package org.urchr.beorganized.api.impl.agenda;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.urchr.beorganized.api.base.Log;
import org.urchr.beorganized.api.base.PrivateBaseAPIServlet;
import org.urchr.beorganized.dao.NotesDAO;
import org.urchr.beorganized.dao.XMLDocumentBuilder;
import org.urchr.beorganized.dao.errors.DBErrorException;
import org.urchr.beorganized.dao.errors.GeneralException;

public class GetAgenda extends PrivateBaseAPIServlet {

	@Override
	protected void doProcessPrivate() throws GeneralException {
		int noteID = NotesDAO.safeInt(getParameter("noteid"), -1);
		boolean appendPaths = "true".equals(getParameter("appendpaths"));
		boolean addTags = "true".equals(getParameter("addtags"));
		boolean lRenderCalendar = "true".equals(getParameter("calendar"));
		Date lCalendarDate = null;
		if(lRenderCalendar){
			try {
				lCalendarDate = new SimpleDateFormat("yyyy-MM-dd").parse(getParameter("calendardate"));
			} catch (Exception e) {
			}
			if(lCalendarDate==null)
				throw new DBErrorException();
		}
		int nextDays = NotesDAO.safeInt(getParameter("nextdays"), 2);
		XMLDocumentBuilder doc = new XMLDocumentBuilder();
		String tagsString = null;
		if(xmlObject!=null)
			tagsString = xmlObject.getAttribute("tagsfilter");
		if(null==tagsString)
			tagsString = "";
//		Log.debug(this, "Tag filter: "+tagsString);
		sendNode(NotesDAO.getTaskList(doc.getRootElement("list"), doc, 
				noteID, userID, appendPaths, addTags, tagsString, nextDays, 
				lRenderCalendar, lCalendarDate));
	}

}
